/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;


import fvm.FVModelData;
import gui.dialogs.StartSim;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import listener.ParentEventListener;


/**
 *
 * @author max
 */
public class GroundWater_GUI extends JFrame implements ActionListener{
    
    
    private int timesteps=0,timestep=0;
    
    JPanel pan_north;
    JPanel pan_south;
    JPanel pan_west;
    JPanel pan_east;
    JPanel pan1;
    JCanvas canvas;
    JColorBar colorbar;
    JButton button1;
    JButton button2;
    JButton button3;
    JButton button4;
    JButton button5;
    JButton button6;
    JButton button7;
    JButton button8;
    JButton button9;
    JButton button10;
    JButton button11;
    JButton button12;
    JButton button13;
    JButton button14;
    JButton button15;
    JButton button16;
    JButton button17;
    
    JLabel lab1;
    JLabel lab2;
    JLabel lab3;
    JLabel lab4;
    JLabel lab5;
    JLabel lab6;
    
    JTextField tf1;
    JTextField tf2;
    JTextField tf3;
    JTextField tf4;
    JTextField tf5;
    

    ParentEventListener parenteventlistener;
    FVModelData fvmd;
    double dt;
    
    public GroundWater_GUI() {
        
        pan_north = new JPanel();
        pan_north.setLayout(new FlowLayout());
        pan_south = new JPanel();
        //pan_south.setLayout(new FlowLayout());
        pan_west = new JPanel();
        pan_west.setLayout(new BoxLayout(pan_west, BoxLayout.Y_AXIS));
        pan_east = new JPanel();
        pan_east.setLayout(new BoxLayout(pan_east, BoxLayout.Y_AXIS));
        pan1 = new JPanel();
        canvas = new JCanvas();
        colorbar = new JColorBar();
        button1 = new JButton("Gebiet erstellen");
        button2 = new JButton("Stützstellen an/aus");
        button3 = new JButton("Assemblieren");
        button4 = new JButton("Simulation");
        button5 = new JButton("Button2");
        button6 = new JButton("Button2");
        button9 = new JButton("FV-Rahmen an/aus");
        button10 = new JButton("Piezeometerhöhen an/aus");
        button11 = new JButton("10 Schritte zurück");
        button12 = new JButton("1 Schritt zurück");
        button13 = new JButton("1 Schritt vor");
        button14 = new JButton("10 Schritte vor");
        button15 = new JButton("Play");
        button16 = new JButton("Stop");
        button17 = new JButton("Pause");
     
        lab1 = new JLabel("Länge");
   
        lab2= new JLabel("Höhe");
    lab3= new JLabel("dx");
     lab4= new JLabel("dy");
     lab5= new JLabel("t: ");
     lab6= new JLabel("");
     
    
     tf1 = new JTextField(5);
     tf2 = new JTextField(5);
     tf3 = new JTextField(5);
     tf4 = new JTextField(5);
     tf5 = new JTextField(5);
    
        
    tf1.setAlignmentX(CENTER_ALIGNMENT);
    tf2.setAlignmentX(CENTER_ALIGNMENT);
    tf3.setAlignmentX(CENTER_ALIGNMENT);
    tf4.setAlignmentX(CENTER_ALIGNMENT);
       
    tf1.setText("20");
    tf2.setText("20");
    tf3.setText("1");
    tf4.setText("1");

        //pan_paint.setBackground(Color.red);
        //pan_paint.setSize(3*this.getHeight()/4, 3*this.getWidth()/4);
        //setExtendedState(JFrame.MAXIMIZED_BOTH);
        setSize(1200, 800);
        setUndecorated(false);
        button3.setEnabled(false);
        button4.setEnabled(false);
        this.setLayout(new BorderLayout());
        button1.addActionListener(this);
        button2.addActionListener(this);
        button9.addActionListener(this);
        button10.addActionListener(this);
        button3.addActionListener(this);
        button4.addActionListener(this);
        button11.addActionListener(this);
        button12.addActionListener(this);
        button13.addActionListener(this);
        button14.addActionListener(this);
        button15.addActionListener(this);
        button16.addActionListener(this);
        button17.addActionListener(this);
        getContentPane().add(pan_north,BorderLayout.NORTH);
        getContentPane().add(colorbar,BorderLayout.SOUTH);
        getContentPane().add(pan_east,BorderLayout.EAST);
        getContentPane().add(pan_west,BorderLayout.WEST);
        getContentPane().add(canvas, BorderLayout.CENTER);
        pan_north.add(button1);
        pan_north.add(button2);
        pan_east.add(button3);
        pan_east.add(button4);
        
                pan_east.add(button15);
                pan_east.add(button16);
                pan_east.add(button17);
        
        
        pan_east.add(button11);
        pan_east.add(button12);
        

        
 
   
       pan_east.add(button13);
        pan_east.add(button14);
        
        pan_west.add(button5);
        pan_west.add(button6);
        pan_north.add(button9);
        pan_north.add(button10);
           pan_north.add(lab1);    
           pan_north.add(tf1);  
           pan_north.add(lab2);    
           pan_north.add(tf2);  
           pan_north.add(lab3);    
           pan_north.add(tf3);  
           pan_north.add(lab4);    
           pan_north.add(tf4);  
        
           pan_north.add(lab5);
pan_north.add(tf5);
        
           fvmd = null;
        
        this.setVisible(true);
        
        
        
        
        this.addWindowListener(new Windowadapt());
        
        canvas.addColorBar(colorbar);
        
        
    }
    
  
    public void addparentEventListener(ParentEventListener eventlistener){
    
    this.parenteventlistener = eventlistener;
    }
    
  
    
    public JColorBar getColorBar(){return colorbar;}

    @Override
    public void actionPerformed(ActionEvent e) {
       
        if(e.getSource().equals(button1)){
 
        
        double sizex = Double.parseDouble(tf1.getText());
        double sizey = Double.parseDouble(tf2.getText());
        double dx = Double.parseDouble(tf3.getText());
        double dy = Double.parseDouble(tf4.getText());
        
        int n = (int)(sizex/dx);
        int m = (int)(sizey/dy);
        
        dx = sizex/n;
        dy = sizey/m;
        
        tf3.setText(Double.toString(dx));
        tf4.setText(Double.toString(dy));
        FVModelData fvmd = new FVModelData(n,m,sizex,sizey,dx,dy);
        parenteventlistener.Event_ModelCreated(fvmd);
            }
    
    if(e.getSource().equals(button2)){
    
    if(canvas.showstützstellen == true)canvas.showstützstellen = false;
    else canvas.showstützstellen = true;
     
    canvas.repaint();
    
    }
    
    if(e.getSource().equals(button9)){
    
            
    if(canvas.showfvframes == true)canvas.showfvframes = false;
    else canvas.showfvframes = true;
     
    canvas.repaint();
    
    }
    
        if(e.getSource().equals(button10)){
    
            
    if(canvas.showfvcolors == true)canvas.showfvcolors = false;
    else canvas.showfvcolors = true;
     
    canvas.repaint();
    
    }
    
    if(e.getSource().equals(button3)){
    
        parenteventlistener.Event_Assemble();
    
    }
    
     if(e.getSource().equals(button4)){
    
        StartSim starts = new StartSim(this);
         double t = starts.gett();
         this.dt = starts.getdt();
         
         parenteventlistener.Event_StartSim(t, this.dt);
    }
    
     
     if(e.getSource().equals(button11)){
         
         if(timestep-10>=0){
         
             int times = timestep-10;
             setTimeStep(times);
             canvas.repaint();
         
         }}
         
             if(e.getSource().equals(button12)){
         
         if(timestep-1>=0){
         
             int times = timestep-1;
             setTimeStep(times);
             canvas.repaint();
             
         
         }
         
     }
             
               if(e.getSource().equals(button13)){
         
         if(timestep+1<=timesteps){
         
             int times = timestep+1;
             setTimeStep(times);
             canvas.repaint();
             
         
         }
         
     }
                 if(e.getSource().equals(button14)){
         
         if(timestep+10<=timesteps){
         
             int times = timestep+10;
             setTimeStep(times);
             canvas.repaint();
             
         
         }
         
     }
    
                            if(e.getSource().equals(button15)){
         
         Thread t1 = new Thread(new Runnable() {

                                    @Override
                                    public void run() {
                                        
                                        while (timestep < timesteps) {                                            
                                            
                                            int time = timestep+1;
                                            setTimeStep(time);
                                            repaint();
                                            try {
                                                Thread.sleep((long)(dt*10));
                                            } catch (InterruptedException ex) {
                                                Logger.getLogger(GroundWater_GUI.class.getName()).log(Level.SEVERE, null, ex);
                                            }
                                        }
                                        
                                    }
                                });
         
         t1.start();
             
         
         }
         
     }
    

    


  
    public void ModelCreate(FVModelData fvmd) {
       this.fvmd = fvmd;
       canvas.setModel(fvmd);
       
    }
    
    public void enableAssem(){button3.setEnabled(true);}
    public void dissableAssem(){button3.setEnabled(false);
    }
    
    public void enableSim(){button4.setEnabled(true);}
    public void dissableSim(){button4.setEnabled(false);
    }
    
    
    private class Windowadapt extends WindowAdapter {
        
        public void windowClosing(WindowEvent event1) {
            System.exit(0);
        }
    }
    
    
    
//    private class GuiEventListen implements GUIEventListener{
//
//        @Override
//        public void test() {
//            System.out.println("answear");
//        }
//
//        @Override
//        public void ModelCreated(FVModelData fvmd) {
//           
//            = fvmd;
//        }
//
//
//
//      
//    
//    
//    }
    
    
    public void setTimeSteps(int timesteps){
        this.timesteps = timesteps;
    canvas.setTimeSteps(timesteps);
    
    }
    public void setTimeStep(int timestep){
        this.timestep = timestep;
    canvas.setTimeStep(timestep);
    tf5.setText(Integer.toString(timestep));
    }
}
